package com.yamed.bus.web;


import com.alibaba.fastjson.JSONObject;
import com.yamed.bus.service.kakfa.KafkaProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/email")
public class EmailController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private KafkaProducer kafkaProducer;
    @Resource
    private HttpServletRequest httpServletRequest;
    @Value("${kafka.topic-email}")
    private String emailTopic;

    /**
     * 发送邮件
     *
     * @return
     */
    @GetMapping("/send")
    public String send() {
        logger.info(String.format("sendEmailStart, topic is %s", emailTopic));
        String toAddress = "zero365365@163.com";
        String title = "测试邮件" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String content = "Hello World ！！！";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("toAddress", toAddress);
        jsonObject.put("title", title);
        jsonObject.put("content", content);
        kafkaProducer.sendMsg(emailTopic, jsonObject);
        logger.info("sendEmailEnd ...");
        return "SUCCESS";
    }

    @GetMapping("/error")
    public String error() {
        logger.error("sendEmailError ...");
        return "error";
    }

}

